package com.toommi.refuse.tools;

import com.toommi.refuse.common.enums.NormalTrashTypeEnum;
import com.toommi.refuse.common.enums.PayStatusEnum;
import com.toommi.refuse.common.enums.RefuseTypeEnum;
import com.toommi.refuse.entity.RecycleRecord;
import com.toommi.refuse.enums.RecycleRecordExceptionEnum;
import com.toommi.refuse.exception.RecycleRecordException;
import org.apache.commons.lang3.StringUtils;

/**
 * ..
 * 参数校验工具
 *
 * @author yangg
 * @since 2019-08-01 11:19
 */
public class ParamCheckUtil {

    private ParamCheckUtil() {
    }

    /**
     * 普通垃圾回收记录校验
     * 一级分类信息必须为normal
     * 二级分类信息不能为空，且必须包含在NormalTrashTypeEnum内
     * weight不能为空|<0
     *
     * @param record 投放记录
     */
    public static void checkNormalRecycleRecord(RecycleRecord record) throws RecycleRecordException {
        //一级类型必须为normal
        if (StringUtils.isEmpty(record.getRefuseType())) {
            throw new RecycleRecordException(RecycleRecordExceptionEnum.INVALID_REFUSE_TYPE_NULL);
        }

        if (record.getSubRefuseTypeId() <= 0 ) {
            throw new RecycleRecordException(RecycleRecordExceptionEnum.INVALID_SUBREFUSE_TYPE_NULL);
        }

        if (record.getWeight() <= 0) {
            throw new RecycleRecordException(RecycleRecordExceptionEnum.INVALID_WEIGHT_FORMAT);
        }

//        if (StringUtils.isEmpty(record.getQrcode())) {
//            throw new RecycleRecordException(RecycleRecordExceptionEnum.INVALID_HOUSEHOLDCODE_EMPTY);
//        }
    }

    /**
     * 可回收物回收记录参数校验
     * 一级分类必须为recyclable
     * 二级分类不能为空
     * app端操作人员id不能为空
     * 重量不能为空或者小于0
     *
     * @param record 可回收物回收记录
     */
    public static void checkRecyclableRecycleRecord(RecycleRecord record) throws RecycleRecordException {

        if (StringUtils.isEmpty(record.getRefuseType()) || !RefuseTypeEnum.RECYCLABLE.getCode().equals(record.getRefuseType())) {
            throw new RecycleRecordException(RecycleRecordExceptionEnum.INVALID_REFUSE_TYPE_NULL);
        }

        if (record.getSubRefuseTypeId() <= 0) {
            throw new RecycleRecordException(RecycleRecordExceptionEnum.INVALID_SUBREFUSE_TYPE_NULL);
        }

        if (record.getWeight() <= 0) {
            throw new RecycleRecordException(RecycleRecordExceptionEnum.INVALID_WEIGHT_FORMAT);
        }
        if (record.getAppUserId() <= 0L) {
            throw new RecycleRecordException(RecycleRecordExceptionEnum.INVALID_APPUSER_NULL);
        }
        if (StringUtils.isEmpty(record.getQrcode())) {
            throw new RecycleRecordException(RecycleRecordExceptionEnum.INVALID_HOUSEHOLDCODE_EMPTY);
        }

        if (!PayStatusEnum.hasValue(record.getPayStatus())) {
            throw new RecycleRecordException(RecycleRecordExceptionEnum.INVALID_PAYSTATUS);
        }
    }

    /**
     * 大件垃圾回收记录校验
     * 一级分类必须为large
     * 二级分类不能为空
     * app端操作人员不能为空
     * 回收价格Money不能小于0
     *
     * @param record 回收记录
     */
    public static void checkLargeRecycleRecord(RecycleRecord record) throws RecycleRecordException {
        if (StringUtils.isEmpty(record.getRefuseType()) || !RefuseTypeEnum.LARGE.getCode().equals(record.getRefuseType())) {
            throw new RecycleRecordException(RecycleRecordExceptionEnum.INVALID_REFUSE_TYPE_NULL);
        }

        if (record.getSubRefuseTypeId() <= 0) {
            throw new RecycleRecordException(RecycleRecordExceptionEnum.INVALID_SUBREFUSE_TYPE_NULL);
        }

        if (record.getMoney().intValue() <= 0) {
            throw new RecycleRecordException(RecycleRecordExceptionEnum.INVALID_WEIGHT_FORMAT);
        }
        if (record.getAppUserId() <= 0L) {
            throw new RecycleRecordException(RecycleRecordExceptionEnum.INVALID_APPUSER_NULL);
        }
        if (StringUtils.isEmpty(record.getQrcode())) {
            throw new RecycleRecordException(RecycleRecordExceptionEnum.INVALID_HOUSEHOLDCODE_EMPTY);
        }
        if (!PayStatusEnum.hasValue(record.getPayStatus())) {
            throw new RecycleRecordException(RecycleRecordExceptionEnum.INVALID_PAYSTATUS);
        }
    }
}
